package com.karaoke.android.kege

import android.app.Fragment
import android.os.Bundle
import android.support.v4.view.ViewCompat
import android.view.MenuItem
import android.view.View
import com.karaoke.android.data.NetworkTools
import com.karaoke.android.data.onUI
import com.karaoke.android.kege.databinding.ActivityKegeBinding
import com.karaoke.android.kege.fragment.KegeFragments
import com.karaoke.android.kege.fragment.SingerDetailFragment
import com.karaoke.android.kege.vm.MainViewModel
import com.karaoke.android.library.mvvm.MvvmActivity
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber
import universum.studios.android.fragment.manage.FragmentController
import universum.studios.android.fragment.manage.FragmentRequest
import universum.studios.android.fragment.manage.FragmentRequestInterceptor
import kotlin.properties.Delegates


class KegeActivity : MvvmActivity<ActivityKegeBinding, MainViewModel>(), FragmentRequestInterceptor
{
    override fun interceptFragmentRequest(request: FragmentRequest): Fragment?
    {
        return null
    }

    private var controller by Delegates.notNull<FragmentController>()

    override val viewModel: MainViewModel by lazy { MainViewModel() }
    override val dataBinding: ActivityKegeBinding by lazy { ActivityKegeBinding.inflate(layoutInflater).apply { this.viewModel = viewModel } }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
        activity = this
        setSupportActionBar(dataBinding.toolbar)
        title = "主页"
        controller = FragmentController(fragmentManager)
        controller.viewContainerId = R.id.container
        controller.factory = KegeFragments()
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
        if (savedInstanceState == null)
            controller.newRequest(KegeFragments.MAIN).immediate(true).execute()
        fragmentManager.addOnBackStackChangedListener { supportActionBar?.setDisplayHomeAsUpEnabled(fragmentManager.backStackEntryCount > 0) }
    }

    fun onCardClick(v: View)
    {
        controller.newRequest(getFragment(v.id))
                .apply { allowStateLoss(true) }
                .transaction(FragmentRequest.REPLACE)
                //.transition(ExtraFragmentTransitions.SCALE_IN_AND_SLIDE_TO_TOP)
                .addToBackStack(true)
                .execute()
    }

    private fun getFragment(id: Int): Int
    {
        when (id)
        {
            R.id.btnHotSong   -> return KegeFragments.HOT_SONGS
            R.id.btnSinger    -> return KegeFragments.SINGER
            R.id.btnLanguage  -> return KegeFragments.SONG_LANGUAGE
            R.id.btnWordCount -> return KegeFragments.WORD_COUNT
            R.id.btnPinYin    -> return KegeFragments.SEARCH
            R.id.btnCategory  -> return KegeFragments.CATEGORY
            R.id.btnBang      -> return KegeFragments.BANG
            else              -> return 0
        }
    }

    fun categoryOnClick(v: View)
    {
        val id = v.tag as String
        NetworkTools.getDictionary(id.toInt()).onUI({
                                                        controller.newRequest(KegeFragments.CATEGORY_DETAIL)
                                                                .apply { allowStateLoss(true);arguments(Bundle().apply { putParcelable("dict", it) }) }
                                                                .transaction(FragmentRequest.REPLACE)
                                                                //.transition(ExtraFragmentTransitions.SCALE_IN_AND_SLIDE_TO_TOP)
                                                                .addToBackStack(true)
                                                                .tag(id)
                                                                .execute()
                                                    })

    }

    fun showArts(v: View)
    {
        controller.newRequest(KegeFragments.CATEGORY_ARTS)
                .transaction(FragmentRequest.REPLACE)
                .addToBackStack(true)
                .execute()
    }

    override fun onDestroy()
    {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }


    @Subscriber
    fun showSingerDetail(e: ShowSingerDetailEvent)
    {
        val avatar = e.view.findViewById(R.id.avatar)
        controller.newRequest(SingerDetailFragment.newInstance(e.singer))
                .transaction(FragmentRequest.REPLACE)
                .sharedElement(avatar, ViewCompat.getTransitionName(avatar))
                .addToBackStack(true)
                .execute()
    }

    @Subscriber(tag = "onSearch")
    fun onSearch(e: String)
    {
        controller.newRequest(KegeFragments.SEARCH)
                .apply { allowStateLoss(true) }
                .transaction(FragmentRequest.REPLACE)
                .addToBackStack(true)
                .execute()
    }

    /**
     * 歌曲点击事件
     */
    fun songItemClick(v: View)
    {

    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean
    {
        when (item?.itemId)
        {
            android.R.id.home -> onBackPressed()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onBackPressed()
    {
        super.onBackPressed()
    }

    companion object
    {
        var activity: KegeActivity by Delegates.notNull()
    }
}
